package xin.nick.rpc.provider.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xin.nick.rpc.api.IUserService;
import xin.nick.rpc.pojo.User;
import xin.nick.rpc.provider.anno.RpcService;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author Nick
 * @date 2021/7/3
 * @description
 */
@RpcService
@Service
public class UserServiceImpl implements IUserService {


    @Value("${rpc.port}")
    private int port;

    Map<Object, User> userMap = new HashMap<>();

    @Override
    public User getById(int id) {
        if(userMap.size() == 0) {
            User user1 = new User();
            user1.setId(1);
            user1.setName("张三" + port);
            userMap.put(user1.getId(), user1);
            User user2 = new User();
            user2.setId(2);
            user2.setName("李四" + port);
            userMap.put(user2.getId(), user2);
        }
        User user = userMap.get(id);
        if(Objects.isNull(user)) {
            user = new User();
        }
        return user;
    }
}
